<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>todoList</title>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="newTask"/>
      <button @click="addTask">添加</button>
      <ul>
        <li v-for="(task,index) in tasks">
          <span v-if="task.completed" style="text-decoration: line-through;" @click="toggle(index)">
            {{task.name}}
          </span>
          <span v-else @click="toggle(index)">
            {{task.name}}
          </span>
          <button @click="removeTask(index)">删除</button>
        </li>
      </ul>
    </div>
    <script src="vue.js"></script>
    <script>
        var vm = new Vue({
            el:'#app',
            data:{
              tasks:[
                {name:'eat',completed:false},
                {name:'drink',completed:true}
              ],
              newTask:''
            },
            methods:{
              addTask(){
                if(this.newTask === '') return;
                this.tasks.push({name:this.newTask,completed:false});
                this.newTask = '';
              },
              toggle(index){
                this.tasks[index].completed=!this.tasks[index].completed;
              },
              removeTask(index){
                this.tasks.splice(index,1);
              }
            }
        });
    </script>
  </body>
</html>